package com.itheima.app.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.im.HuanXinTemplate;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Friend;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.FriendService;
import com.itheima.vo.ContactVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserCommentVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class MessageManager {

    @DubboReference
    UserInfoService userInfoService;

    @DubboReference
    QuestionService questionService;

    @Autowired
    HuanXinTemplate huanXinTemplate;

    @DubboReference
    FriendService friendService;

    @DubboReference
    CommentService commentService;

    public void replyStrangerQuestion(Long recommendUserId, String reply) {
        //1.获取当前用户的id和userInfo
        Long userId = UserHolder.get().getId();
        UserInfo userInfo = userInfoService.findById(userId);

        //2.远程调用questionService获取推荐用户的问题
        Question question = questionService.findByUserId(recommendUserId);
        if (question==null){
            question = new Question();
        }

        //3.调用环信模板发送信息
        Map<String,Object> map = new HashMap<>();
        /*
         "userId":1,
         "huanXinId":"HX1",
         "nickname":"梦里的画",
         "strangerQuestion":"朋友对你来说意味着什么？",
         "reply":"吃饭不用我买单,帮助不用说谢谢"
         */
        map.put("userId",userId);
        map.put("huanXinId","HX"+userId);
        map.put("nickname",userInfo.getNickname());
        map.put("strangerQuestion",question.getStrangerQuestion());
        map.put("reply",reply);


        huanXinTemplate.sendMsg("HX"+recommendUserId, JSON.toJSONString(map));
    }

    public void addContact(Long friendId) {
        //1.获取当前用户id
        Long userId = UserHolder.get().getId();

        //2.远程调用service完成添加好友和动态操作
        friendService.addContact(userId,friendId);

        //3.调用环信模板完成好友添加操作
        huanXinTemplate.addContacts("HX"+userId,"HX"+friendId);
    }

    public ResponseEntity findContactsByPage(Integer pageNum, Integer pageSize) {
        //1.获取用户id,远程调用service获取好友数据
        PageBeanVo pageBeanVo = friendService.findContactsByPage(UserHolder.get().getId(), pageNum, pageSize);

        //2.获取pageBeanVo中好友list
        List<Friend> items = (List<Friend>) pageBeanVo.getItems();

        //3.遍历好友list,获取每个好友的userInfo,将其封装ContactVo,将每个vo放入voList
        List<ContactVo> voList = new ArrayList<>();

        if (CollUtil.isNotEmpty(items)) {
            items.forEach(friend -> {
                Long friendId = friend.getFriendId();
                UserInfo userInfo = userInfoService.findById(friendId);

                ContactVo vo = new ContactVo();
                vo.setUserInfo(userInfo);

                //注意:userId是环信id
                vo.setUserId("HX"+friendId);

                voList.add(vo);
            });
        }

        //4.给pageBeanVo设置voList,且返回
        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findCommentsByPage(Integer pageNum, Integer pageSize, Integer commentType) {
        //1.获取当前用户id
        Long publishUserId = UserHolder.get().getId();

        //2.远程调用service获取当前页的数据
        PageBeanVo pageBeanVo = commentService.findByPage(publishUserId, pageNum, pageSize, commentType);

        //3.获取pageBeanVo中comment集合
        List<Comment> items = (List<Comment>) pageBeanVo.getItems();

        //4.遍历集合,获取每个操作人的userInfo,将其封装成UserCommentVo,将每个vo放入voList中
        List<UserCommentVo> voList = new ArrayList<>();

        if (CollUtil.isNotEmpty(items)) {
            items.forEach(comment -> {
                UserInfo userInfo = userInfoService.findById(comment.getUserId());

                UserCommentVo vo = new UserCommentVo();
                vo.setId(userInfo.getId().toString());
                vo.setAvatar(userInfo.getAvatar());
                vo.setNickname(userInfo.getNickname());
                vo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated())));

                voList.add(vo);
            });
        }

        //5.给pageBeanVo设置volIst且返回
        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }
}
